﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GnToolControls.Convert
{
	public class ConvertFileItem : ConvertItem
	{
		static FileConvert fileConvert = new FileConvert();
		static String _xmlElementName = "FileItem";
		static String _srcXmlElementName = "SrcPath";
		static String _destXmlElementName = "DestPath";
		
		String _srcPath;
		public String SrcPath
		{
			get
			{
				return _srcPath;
			}
			set
			{
				_srcPath = value;
			}
		}

		String _destPath;
		public String DestPath
		{
			get
			{
				return _destPath;
			}
			set
			{
				_destPath = value;
			}
		}

		public ConvertFileItem()
		{

		}

		public ConvertFileItem( String srcPath, String destPath )
		{
			_srcPath = srcPath;
			_destPath = destPath;
		}

		public override void Execute()
		{
			if( fileConvert.Copy( SrcPath, DestPath ) == false )
			{
				String errorLog = String.Format(
					"File Copy Failed source path = {0} dest path = {1}", SrcPath, DestPath );
			}
		}
		public override bool IsEqual( ConvertItem convertSource )
		{
			ConvertFileItem fileSource = convertSource as ConvertFileItem;

			if( fileSource == null )
				return false;

			if( SrcPath.Equals( fileSource.SrcPath ) == false
				|| DestPath.Equals( fileSource.DestPath ) == false )
				return false;

			return true;
		}

		public override void SaveXml( System.Xml.XmlWriter writer )
		{
			writer.WriteStartElement( _xmlElementName );
			writer.WriteElementString( _srcXmlElementName, SrcPath );
			writer.WriteElementString( _destXmlElementName, DestPath );
			writer.WriteEndElement();
		}

		public static ConvertItem LoadXml( System.Xml.XmlReader reader, String findName )
		{
			if( findName != _xmlElementName )
				return null;

			try
			{
				ConvertFileItem item = new ConvertFileItem();
				reader.Read();
				if( reader.IsStartElement( _srcXmlElementName ) )
				{
					reader.Read();
					item.SrcPath = reader.ReadString();
				}
				reader.Read();
				if( reader.IsStartElement( _destXmlElementName ) )
				{
					reader.Read();
					item.DestPath = reader.ReadString();
				}
				return item;
			}
			catch
			{

			}
			//reader.findName
			return null;
		}
	}
}
